<?php

 header('content-type: text/html; charset=utf-8');

include_once 'fire.php';

function insert_data($time = '' ,$temperature = '', $smoke = '')
{
	include_once 'DBLink.php';
	$dblink=new DBLink();
	$conn=$dblink->getConnect();
	$sql2="INSERT INTO fire(time,temperature,smoke)
		value('$time','$temperature','$smoke')";
	if($dblink->query($sql2,$conn))
	{
		printf("%s :insert data into database successfully！\n\n",date("Y-m-d H:i:s"));
	}
	else
	{
		printf("%s insert data into database failure！\n\n",date("Y-m-d H:i:s"));
	}

}

$host = '0.0.0.0';
$port = '8889';
$listen_socket= socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if( $listen_socket < 0)
{
	printf("%s create socket fail!\n\n",date("Y-m-d H:i:s"));
	return false;

}
if((socket_bind($listen_socket,$host, $port)) < 0)
{
	printf("%s socket bind fail!\n\n",date("Y-m-d H:i:s"));
	return false;
}
if ((socket_listen( $listen_socket)) < 0)
{
	printf("%s :socket listen fail!\n\n",date("Y-m-d H:i:s"));
	return false;

}
$client = [$listen_socket];
$write = [];
$exp = [];

$buzzer = 11;

// 开始进入循环
while(true)
{
        $read = $client;
        // 当select监听到了fd变化，注意第四个参数为null
        // 如果写成大于0的整数那么表示将在规定时间内超时
        // 如果写成等于0的整数那么表示不断调用select，执行后立马返回，然后继续
        // 如果写成null，那么表示select会阻塞一直到监听发生变化
        if( socket_select( $read, $write, $exp, null ) > 0 )
        {
                // 判断listen_socket有没有发生变化，如果有就是有客户端发生连接操作了
                if(in_array( $listen_socket, $read ) )
                {
                        // 将客户端socket加入到client数组中
                        $client_socket = socket_accept($listen_socket);
                        if( $client_socket <= 0)
			{
                                printf("%s :connect client failure!\n\n",date("Y-m-d H:i:s"));
                        }
                        printf("%s : connect client successfully!\n\n",date("Y-m-d H:i:s"));
                        $client[] = $client_socket;
                        // 然后将listen_socket从read中去除掉
                        $key = array_search( $listen_socket, $read );
			unset( $read[ $key ]);
                }
                // 查看去除listen_socket中是否还有client_socket
                if( count($read) > 0 )
                {
                        foreach( $read as $socket_item )
                        {
				$content = socket_read($socket_item,2048);
				if($content == NULL)
				{
					printf("%s : The client is disconnected!\n\n",date("Y-m-d H:i:s"));
					sleep(10);
					break;
				}
				date_default_timezone_set("PRC");
				printf("%s : %s \n\n",date("Y-m-d H:i:s"),$content);
				$result = json_decode($content,true);
				if($result == NULL)
				{

					printf("%s: decode json fail!\n\n",date("Y-m-d H:i:s"));
					sleep(5);
					continue;
				}
				insert_data($result['time'], $result['temperature'], $result['smoke']);
				$temp=(int)$result['temperature'];
				if($temp > 50 || $result['smoke'] == 1)
				{
					$fire = is_fire();
					if($fire == 1)
					{
						include_once 'set_email.php';
						set_email();
						$buzzer = 10;
					}
				}

			}
//			foreach( $client as $client_socket )
//			{
//				// 因为client数组中包含了 listen_socket 以及当前发送者自己socket，所以需要排除二者
//				if( $client_socket != $listen_socket && $client_socket != $socket_item )
//				{
//					socket_write( $client_socket, $buzzer, strlen($buzzer));
//				}
//
//			}
		}
        // 当select没有监听到可操作fd的时候，直接continue进入下一次循环
        else
	{
		sleep(1);
                continue;
        }
	}
}

?>

